<?php namespace Phpcmf\Controllers;

class Buyjiugetujifen extends \Phpcmf\Home\Mform {
	// 继承类初始化
	protected function _Extend_Init() {
	    // 初始化模块
	    $this->_module_init(APP_DIR);
	
	    // 判断表单是否操作
	    $this->form = $this->module['form']['son'];
	    if (!$this->form) {
	        $this->_msg(0, dr_lang('模块表单【%s】不存在',\Phpcmf\Service::L('Router')->class));
	    }
	
	    // 支持附表存储
	    $this->is_data = 1;
	    // 模板前缀(避免混淆)
	    $this->tpl_name = $this->form['table'];
	    $this->tpl_prefix = 'mform_';
	
	    // 初始化数据表
	    $this->_init([
	        'table' => dr_module_table_prefix(APP_DIR).'_form_'.$this->form['table'],
	        'field' => $this->form['field'],
	        'show_field' => 'title'
	    ]);

	}
	
	/**
	 * 提交支付账单
	 */
	public function jiugetujifen() {
	
	    if (IS_POST) {
	        $scorelog = \Phpcmf\Service::L('input')->post('scorelog');
	        $scorelog['uid'] = (int)$this->member['uid'];
	        $scorelog['username'] = (string)$this->member['username'];
	        $money = floatval($scorelog['money']);
	        if (!$money) {
	            $this->_msg(0, dr_lang('金额(%s)不正确', $money));
	            exit;
	        }
			//dr_debug('scorelog', $scorelog);
			
			/*$tianshus = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_form_son')->where('id', $scorelog['quanid'])->get()->getRowArray();
			$tianshu = (int)$tianshus['youxiaoshijian'];
			if ($tianshu == 0) {
				$tianshu = 30000;
			} else {
				$tianshu = $tianshu;
			}*/
			$shijianid = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_tupian_buy')->where('cid', $scorelog['id'])->where('uid', $this->uid)->get()->getRowArray();
			
			if ($shijianid) {
			    $this->_msg(0, dr_lang('该账单已支付'));
			} else {
				if ($this->member['score'] < $money) {
					$this->_msg(0, ''.SITE_SCORE.'不够了');
				}
				// 新增记录表
				\Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_tupian_buy')->insert(array(
				    'cid' => $scorelog['id'],
				    'uid' => $this->uid,
					'inputtime' => SYS_TIME,
					'jiangli' => $money,
					'endtime' => SYS_TIME + $tianshu * 3600 * 24,
				));
			}
			
			
			$urls = 'index.php?s='.$scorelog['s'].'&c=son&id='.$scorelog['id'];
			\Phpcmf\Service::M('member')->add_score($this->uid, -$money, $note = '积分阅读', $urls, $mark = '', $count = 0, $scorelog['id']);
	       
	        $url = PAY_URL.'index.php?s='.$scorelog['s'].'&c=son&id='.$scorelog['id'];
	        if (IS_API_HTTP || (\Phpcmf\Service::L('input')->get('is_ajax') || IS_API_HTTP || IS_AJAX)) {
	            // 回调页面
	            $this->_json($rt['code'], $url, '消费成功');
	        } else {
	            // 跳转到支付页面，必须跳转到统一的主域名中付款
	            dr_redirect($url, 'auto');
	        }
	        exit;
	    } else {
	        $this->_msg(0, dr_lang('POST请求错误'));
	    }
	}


    public function index() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        if (!$id) {
            exit($this->_msg(0, dr_lang('id参数不完整')));
        } elseif (!$this->uid) {
            $this->_msg(0, dr_lang('登录之后才能购买'));
        }
		
		$dateid = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_form_son')->get($id);
		//dr_debug('文件名称', $dateid);
        if (!$dateid) {
            $this->_msg(0, dr_lang('帖子id不存在'));
        } elseif ($dateid['uid'] == $this->uid) {
            $this->_msg(0, dr_lang('不能购买自己的'));
        } elseif (\Phpcmf\Service::M('buyjiugetu', MOD_DIR)->is_buy($this->uid, $id)) {
			/*$tianshus = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $id)->get()->getRowArray();
			$tianshu = (int)$tianshus['yxsj'];
			if ($tianshu == 0) {
				//$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
			} else {
				
				$daoqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_tupian_buy')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
				//dr_debug('$daoqi', $daoqi);
				if ($daoqi['endtime'] < time()) {
					
				} else {
					$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
				}
			}*/
			$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
				
			
        }
		//$yue = dr_payform('my-'.MOD_DIR.'_buyjiugetujifen-'.$id);
		//dr_debug('文件名称', $yue);

        \Phpcmf\Service::V()->assign([
            'payform' => dr_payform('my-'.MOD_DIR.'_buyjiugetujifen-'.$id),
            'meta_title' => dr_lang('在线付款').SITE_SEOJOIN.SITE_NAME,
            'meta_keywords' => $this->get_cache('site', SITE_ID, 'config', 'SITE_KEYWORDS'),
            'meta_description' => $this->get_cache('site', SITE_ID, 'config', 'SITE_DESCRIPTION')
        ]);
        \Phpcmf\Service::V()->display('buyjiugetujifen.html');
    }
}
